package com.arnold.www.singleton;

/**
 * @ClassName Singleton_lazyLoading_synchronized
 * @Description: 懒汉式（用synchronized多线程下两种锁机制偏向锁，和轻量级锁（自旋）在一定情况下影响性能，最终向重度锁发展）
 * @Author Arnold
 * @Date 2020/4/11
 * @Version V2.0
 **/
public class Singleton_lazyLoading_synchronized {
    //私有化构造方法
    private Singleton_lazyLoading_synchronized(){}

    private static Singleton_lazyLoading_synchronized instance;

    //对外暴露获取实例方法
    public static synchronized  Singleton_lazyLoading_synchronized getInstance() {
        //轻量级锁会在这里形成自旋，影响性能
        if (instance==null){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return instance = new Singleton_lazyLoading_synchronized();
        }
        return  instance;
    }
}